/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.capex.farmacus.Manager;

import edu.capex.farmacus.controllers.VendedorfarmaciamedicamentoJpaController;
import edu.capex.farmacus.encapsulates.Vendedorfarmaciamedicamento;
import edu.capex.farmacus.encapsulates.reportes.ReportesVendedorYMedicina;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author IK
 */
public class VendedorFarmaciaMedicamentoManager extends VendedorfarmaciamedicamentoJpaController implements Serializable {

    public VendedorFarmaciaMedicamentoManager(EntityManagerFactory emf) {
        super(emf);
    }

    public VendedorFarmaciaMedicamentoManager() {
        super(Persistence.createEntityManagerFactory("farmacusPU"));
    }

    public List<Vendedorfarmaciamedicamento> findEntitiesVendedoresFarmaciaMedicamentoFilteredByVendedor(Integer idVendedor) {
        List<Vendedorfarmaciamedicamento> listaVendedores = null;
        EntityManager em = getEntityManager();

        try {
            Query query = em.createNamedQuery("Vendedorfarmaciamedicamento.findByVendedor");
            query.setParameter("vendedor", idVendedor);
            listaVendedores = query.getResultList();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            em.close();
        }

        return listaVendedores;
    }

    public List<ReportesVendedorYMedicina> findEntitiesVendedoresYFarmacia() {
        List<ReportesVendedorYMedicina> listaVendedores = new ArrayList<ReportesVendedorYMedicina>();
        EntityManager em = getEntityManager();

        try {
            Query query = em.createQuery("SELECT v.vendedores.nombre, v.farmacias.nombre, SUM(v.cobro), SUM(v.venta) FROM Vendedorfarmaciamedicamento v GROUP BY v.vendedores, v.farmacias");
            List resultList = query.getResultList();
            for (Iterator it = resultList.iterator(); it.hasNext();) {
                Object[] fila = (Object[]) it.next();
                ReportesVendedorYMedicina temp = new ReportesVendedorYMedicina();
                temp.setVendedor(fila[0].toString());
                temp.setFarmacia(fila[1].toString());
                temp.setCobro(new Integer(fila[2].toString()));
                temp.setVenta(new Integer(fila[3].toString()));
                listaVendedores.add(temp);

            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            em.close();
        }

        return listaVendedores;
    }
    
    public List<ReportesVendedorYMedicina> findEntitiesMedicinaYFarmacia() {
        List<ReportesVendedorYMedicina> listaVendedores = new ArrayList<ReportesVendedorYMedicina>();
        EntityManager em = getEntityManager();

        try {
            Query query = em.createQuery("SELECT v.medicamentos.nombreMedicamento, v.farmacias.nombre, SUM(v.cobro), SUM(v.venta) FROM Vendedorfarmaciamedicamento v GROUP BY v.vendedores, v.farmacias");
            List resultList = query.getResultList();
            for (Iterator it = resultList.iterator(); it.hasNext();) {
                Object[] fila = (Object[]) it.next();
                ReportesVendedorYMedicina temp = new ReportesVendedorYMedicina();
                temp.setVendedor(fila[0].toString());
                temp.setFarmacia(fila[1].toString());
                temp.setCobro(new Integer(fila[2].toString()));
                temp.setVenta(new Integer(fila[3].toString()));
                listaVendedores.add(temp);

            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            em.close();
        }

        return listaVendedores;
    }

    public static void main(String[] args) {
        new VendedorFarmaciaMedicamentoManager();
    }
}
